const util = require("../../utils/util.js");
const app = getApp();
Page({
  data: {
    title: "任务",
    isTransform: false,
    isBack: false,
    date: '',
    empty: true,
    loading: false,
    minDate: new Date(2021, 1, 1).getTime(),
    maxDate: new Date(2025, 12, 31).getTime(),
    mission: [],
    logining: false
  },
  bindDateChange: function (event) {
    let that = this
    if(util.isLoged()){
      that.getMissionList(event.detail.value)
      console.log(event.detail.value);
      that.setData({
        date: event.detail.value,
        loading: false,
        empty: true
      })
    } else {
      that.setData({
        logining: 'show'
      })
    }

  },

  addMession() {
    let that = this
    if (util.isLoged()) {
      wx.navigateTo({
        url: '../addmission/addmission',
      })
    } else {
      that.setData({
        logining: 'show'
      })
    }
  },
  getMissionList: function (date) {
    let that = this
    let dates = date
    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        wx.cloud.callFunction({
          // 需调用的云函数名
          name: 'getMissionList',
          // 传给云函数的参数
          data: {
            all: false,
            openid: res.result.openid,
            date: dates,
          },
          success: function (res) {
            wx.stopPullDownRefresh()
            if (res.result.data.length > 0) {
              that.setData({
                mission: res.result.data,
                empty: true,
              })
            } else {
              that.setData({
                mission: res.result.data,
                empty: false,
              })
            }
            that.setData({
              loading: true
            })
          },
          fail: function (err) {
            console.log(err);
          }
        })
      }
    })
  },
  onLoad: function (options) {
    let that = this;
    let date = util.formatDate(new Date())[0]
    that.setData({
      date: date
    });
    that.getMissionList(date)
  },
  onShow: function () {
    let that = this;
    let date = util.formatDate(new Date())[0]
    that.setData({
      date: date,
      loading: false,
      empty: true
    });
    that.getMissionList(date)
  }
})